# -*- coding:utf-8 -*-
# @Time : 2020/10/23 12:01
# @Author: limeng
# @File : add_task_operate_log.py
import time

from application import db
from application.models.models import AddtaskOperateLog


def task_operate_log(typevalue, result, task_operate_log, task_id, user_id, operate_old_dic, operate_now_dic,
                     operate_fields):
    """
    定时任务用户操作日志记录
    :param operate_fields: 修改字段
    :param operate_now_dic: 修改之前的值dic
    :param operate_old_dic: 修改之后的值dic
    :param user_id: 操作人id
    :param typevalue: 操作类型
    :param result: 操作结果
    :param task_operate_log: 操作内容记录
    :param task_id: 关联的任务id
    :return: 布尔值
    """
    task_olog_list = []
    if operate_fields:
        for i in operate_fields:
            task_olog = AddtaskOperateLog()
            task_olog.task_operate_user = user_id
            task_olog.task_operate_type = typevalue
            task_olog.task_operate_result = result
            task_olog.task_operate_log = task_operate_log
            task_olog.task_operate_time = time.strftime(
                "%Y-%m-%d %H:%M:%S",
                time.localtime())
            task_olog.addtask_task_id = task_id
            task_olog.operate_fields = i
            task_olog.operate_old = operate_old_dic[i]
            task_olog.operate_now = operate_now_dic[i]
            task_olog_list.append(task_olog)
    else:
        task_olog = AddtaskOperateLog()
        task_olog.task_operate_user = user_id
        task_olog.task_operate_type = typevalue
        task_olog.task_operate_result = result
        task_olog.task_operate_log = task_operate_log
        task_olog.task_operate_time = time.strftime(
            "%Y-%m-%d %H:%M:%S",
            time.localtime())
        task_olog.addtask_task_id = task_id
        task_olog.operate_fields = ""
        task_olog.operate_old = ""
        task_olog.operate_now = ""
        task_olog_list.append(task_olog)
    try:
        db.session.add_all(task_olog_list)
        db.session.commit()
        # db.session.close()
        return True
    except:
        db.session.rollback()
        return False


def sel_tasklog(data):
    """
    查询任务操作日志
    :param data: 每页多少条/页码/任务id
    :return: list
    """
    id = data['id']
    page_num = data['page_num']
    page_per = data['page_per']
    page_data = db.session.query(AddtaskOperateLog).filter(AddtaskOperateLog.addtask_task_id == id).order_by(
        AddtaskOperateLog.task_operate_time.desc()).paginate(
        page=page_num,
        per_page=page_per, error_out=False)
    res_log_dic = {}
    res_log_list = []
    if page_data.items:
        for msg in page_data.items:
            res_log_dic['id'] = msg.id
            res_log_dic['task_operate_user'] = msg.task_operate_user
            res_log_dic['task_operate_time'] = msg.task_operate_time
            res_log_dic['task_operate_type'] = msg.task_operate_type
            res_log_dic['task_operate_result'] = msg.task_operate_result
            res_log_dic['task_operate_log'] = msg.task_operate_log
            res_log_dic['operate_old'] = msg.operate_old
            res_log_dic['operate_now'] = msg.operate_now
            res_log_dic['operate_fields'] = msg.operate_fields
            res_log_list.append(res_log_dic.copy())
            res_log_dic.clear()
    res_log_dic['items'] = res_log_list
    res_log_dic['total'] = page_data.total
    return res_log_dic
